package com.pwc.conversionTool.rest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import org.dom4j.DocumentException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pwc.conversionTool.XML.XMLFactory;
import com.pwc.conversionTool.XML.XMLNode;
import com.pwc.conversionTool.biz.DataAdaptorBiz;
import com.pwc.conversionTool.biz.ProjectBiz;
import com.pwc.conversionTool.entity.Operation;
import com.pwc.conversionTool.entity.Project;

import static com.pwc.conversionTool.controller.sessionPersistence.AccountSessionPersistence.*;

@Controller
public class ProjectRest {
	private static final Logger logger = LoggerFactory.getLogger(ProjectRest.class);

	private ProjectBiz projectBiz = new ProjectBiz();

	private DataAdaptorBiz dataAdaptorBiz = new DataAdaptorBiz();

	@Autowired
	public ServletContext context;

	@RequestMapping(value = "/rest/project/deleteProject", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody boolean createProjectRest(@RequestBody Project project, HttpSession session) {

		try {
			projectBiz.deleteProjectById(project.getId());
		} catch (Exception e) {
			
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));
			return false;
		}

		return true;

	}

	@RequestMapping(value = "/rest/project/getAllProject", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody List<Project> getAllProject(HttpSession session) {

		List<Project> projects = new ArrayList<Project>();

		try {
			projects = projectBiz.getProjectByAccount(getCurrentAccount(session));
		} catch (Exception e) {
			
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));
		}

		return projects;
	}

	@RequestMapping(value = "/rest/project/getOperationsByProjectId", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody List<Operation> getOperationsByProjectId(@RequestBody Project project, HttpSession session) {

		List<Operation> operations = new ArrayList<Operation>();

		try {
			operations = projectBiz.getOperationsByProject(project);
		} catch (Exception e) {
			
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));
		}

		return operations;
	}

	@RequestMapping(value = "/rest/project/deleteOperation", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody boolean deleteOperation(@RequestBody Long id, HttpSession session) {

		projectBiz.deleteOperationById(id);

		return true;

	}

	@RequestMapping(value = "/rest/project/getOperationById", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Operation getOperationById(@RequestBody Long id, HttpSession session) {

		Operation operation = null;

		try {
			operation = projectBiz.getOperationById(id);
		} catch (Exception e) {
			
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));
		}

		return operation;
	}

	@RequestMapping(value = "/rest/project/getHeaderByOperationById", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody String[] getHeaderByOperationById(@RequestBody Long id, HttpSession session) {

		String[] header = null;

		try {
			Operation operation = projectBiz.getOperationById(id);

			header = operation.getDataAdaptorData().getHeader();

		} catch (Exception e) {
			
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));
		}

		return header;
	}

	@RequestMapping(value = "/rest/project/XMLNode", method = RequestMethod.POST, produces = "application/json")
	public @ResponseBody XMLNode getSimpleXMLJson(@RequestBody Long id) {

		XMLNode node = null;
		try {
			Operation operation = projectBiz.getOperationById(id);

			String soapTemplate = operation.getSoapTemplate();

			XMLFactory factory = new XMLFactory(soapTemplate);// xml string

			node = factory.getRootNode();

		} catch (Exception e) {
			
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));
		}

		return node;
	}

	@RequestMapping(value = "/rest/project/saveMapping", method = RequestMethod.POST, produces = "application/json")
	public @ResponseBody boolean saveMapping(@RequestBody Operation operation) {
		try {
			projectBiz.saveMapping(operation);
		} catch (Exception e) {
			
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));
			return false;
		}
		
		
		return true;
	}

}
